<?php
namespace Home\Controller;

use Common\Library\BusinessException;
use Common\Library\SendSMS;
use Common\Library\Constant;
use Common\Library\Http\Request;
use Common\Library\Http\Response;
use Think\Controller;
use Think\Exception;
use Think\Log;

class MerchantBankInfoController extends Controller
{

    public function __construct()
    {
        parent::__construct();

        try {
            Request::parse();
        } catch (BusinessException $e) {
            $ret = ['code' => $e->getCode(), 'msg' => $e->getMessage()];
            $this->ajaxReturn($ret);
        }
    }

    /********************************************
     * 功能描述：获取商户的银行卡列表
     * 创建时间：2016-8-19
     ********************************************/
    public function getMerchantBankList()
    {
        try{
            //0.参数有效性校验
            $merchantId = I('post.merchant_id', '', 'string');
            if(!$merchantId){
                BusinessException::throwException(Constant::INVALID_PARAMETER);
            }
            //1.商户校验
            $sql = 'SELECT t.id FROM merchant t LEFT JOIN merchant_user u ON t.id = u.merchant_id WHERE t.id = '.$merchantId;
            $res = M()->query($sql);
            if(!$res){
                BusinessException::throwException(Constant::MERCHANT_NOT_FOUND);
            }
            //2.获取商户的银行卡列表
            $bankList = [];
            $bank = M('MerchantBankInfo')->where(['merchant_id'=>$merchantId])->field('card_num, bank_id')->select();
            foreach ($bank as $k=>$v){
                $bankList[$k]['card_num'] = $v['card_num'];
                $bankList[$k]['bank_name'] = D('MerchantBankInfo')->getBankName($v['bank_id']);
            }
            $ret = Constant::getReturn(Constant::SUCCESS);
            $ret['data'] = $bankList;
        }catch (BusinessException $e){
            $ret = ['code' => $e->getCode(), 'msg' => $e->getMessage()];
        }

        Response::format($ret);
        $this->ajaxReturn($ret);
    }

    /********************************************
     * 功能描述：获取商户的银行卡列表
     * 创建时间：2016-8-19
     ********************************************/
    public function unbindBankCard()
    {
        try{
            //0.参数有效性校验
            $merchantId = I('post.merchant_id', '', 'string');
            $payPass = I('post.pay_pass', '', 'string');
            $bankCard = I('post.bank_card', '', 'string');
            if(!$merchantId || !$payPass || !$bankCard){
                BusinessException::throwException(Constant::INVALID_PARAMETER);
            }
            //1.商户校验、支付密码校验
            $sql = 'SELECT u.password FROM merchant t LEFT JOIN merchant_user u ON t.id = u.merchant_id WHERE t.id = '.$merchantId;
            $res = M()->query($sql);
            if(!$res){
                BusinessException::throwException(Constant::MERCHANT_NOT_FOUND);
            }
            /*if(!password_unmatched($payPass, $res['pay_pass'])){
                BusinessException::throwException(Constant::PASSWORD_NOT_MATCH);
            }*/
            //2.银行卡校验
            $unband = false;
            $bank = M('MerchantBankInfo')->where(['merchant_id'=>$merchantId])->field('card_num, bank_id')->select();
            foreach ($bank as $k=>$v){
                if($bankCard == $v['card_num']){
                    $unband = true;
                    break;
                }
            }
            //3.接触银行卡绑定
            if($unband){
                //直接删除商户的银行卡数据
                if(false === M('MerchantBankInfo')->where(['merchant_id'=>$merchantId, 'card_num'=>$bankCard])->delete()){
                    BusinessException::throwException(Constant::FAILED);
                }
            }
            $ret = Constant::getReturn(Constant::SUCCESS);
        }catch (BusinessException $e){
            $ret = ['code'=>$e->getCode(), 'msg'=>$e->getMessage()];
        }

        Response::format($ret);
        $this->ajaxReturn($ret);
    }

    public function getBankInfo()
    {
        // M("bank_panduan_new",null)->field("gate_id, bank_name")->where("cut_num = '$cut_num'")->select(); 
      // echo json_encode(['a'=>'a','b'=>'b']);
       try {
            $bankList = [];
            $bankInfo = M("bank_panduan_new",null)->field("gate_id, bank_name, cut_num")->select();
            $matches = [];
            foreach ($bankInfo as $key => $value) {
                $cut_num = $value['cut_num'];
                $bank_name = $value['bank_name'];
                $gate_id = $value['gate_id'];

                // preg_match_all('/[\u4e00-\u9fa5]+/', $value['bank_name'],$matches);
                // $value['bank_name'] = implode($matches);
              //  $value['bank_name'] =substr($value['bank_name'], 0 ,mb_strpos($value['bank_name'], "\n")) ;
                
                $bankList[$cut_num]['bank_name'] = $value['bank_name'];//$matches$value['bank_name']
                $bankList[$cut_num]['gate_id']   = $value['gate_id'];
            }
            $card_num = I("post.card_num");
          //  $card_num = I("get.card_num");
            Log::write("====卡Bin|post|card_num|".$card_num);

            
              
            $bankInfo = $this->bankInfo($card_num, $bankList);
           // print_r($bankInfo);die;   
           //   Log::write("====卡Bin|bankInfo|". var_dump($bankInfo));
            if (is_array($bankInfo)) {
                $ret = ['code'=>0, 'msg'=>'success', 'data'=>$bankInfo ];
            } else {
                BusinessException::throwException("该卡号信息暂未录入");
            }
        } catch (BusinessException $e) {
            $ret = ['code'=>201, 'msg'=>$e->getMessage()];
        }
      //  echo json_encode($ret);
      //  var_dump($ret);
        Log::write("====卡Bin|ret|".json_encode($ret));
        $this->ajaxReturn($ret);
        // bankInfo('6228481552887309119',$bankList);
    }

    public function bankInfo($card_num,$bankList) 
    {
      $card_8 = substr($card_num, 0, 8); 
      if (isset($bankList[$card_8])) { 
        // echo ; 
        return $bankList[$card_8];
      }
      $card_7 = substr($card_num, 0, 7); 
      if (isset($bankList[$card_7])) { 
        // echo ; 
        return $bankList[$card_7];
      }
      $card_6 = substr($card_num, 0, 6); 
      if (isset($bankList[$card_6])) { 
        // echo ; 
        return $bankList[$card_6];
      }
      $card_5 = substr($card_num, 0, 5); 
      if (isset($bankList[$card_5])) { 
        // echo ; 
        return $bankList[$card_5];
      }
      $card_4 = substr($card_num, 0, 4); 
      if (isset($bankList[$card_4])) { 
        // echo ; 
        return $bankList[$card_4];
      }
      $card_3 = substr($card_num, 0, 3); 
      if (isset($bankList[$card_3])) { 
        // echo ; 
        return $bankList[$card_3];
      }
      return '该卡号信息暂未录入'; 
    }
}
